package gsw.tool.skin.utils;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.os.Build;

import gsw.tool.skin.R;

/**
 * Created by siwei on 2018/3/18.
 */

/**
 * 主图资源ID工具类
 */
public class SkinThemeUtils {
    private static int[] TYPEFACE_ATTR = {R.attr.skinTypeface};

    //状态栏颜色与导航栏颜色
    private static int[] STATUSBAR_COLOR_ATTRS = {
            //系统设定的状态栏颜色属性
            android.R.attr.statusBarColor,
            //系统设定的导航栏颜色属性
            android.R.attr.navigationBarColor,
            //V7中系统设定的状态栏颜色属性,在没有设置statusBarColor的情况下使用
            android.support.v7.appcompat.R.attr.colorPrimaryDark};

    /**
     * 根据属性ID获取对应资源ID
     *
     * @param context
     * @param attrs
     * @return
     */
    public static int[] getResID(Context context, int[] attrs) {
        int[] resIDs = new int[attrs.length];
        TypedArray typedArray = context.obtainStyledAttributes(attrs);
        for (int i = 0; i < typedArray.length(); i++) {
            resIDs[i] = typedArray.getResourceId(i, 0);
        }
        typedArray.recycle();
        return resIDs;
    }

    /**
     * 修改状态栏和导航栏颜色
     *
     * @param activity
     */
    public static void updateStatusBar(Activity activity) {
        //5.0以上才能修改
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            return;
        }
        //获取状态栏和导航栏的资源ID
        int[] resIds = getResID(activity, STATUSBAR_COLOR_ATTRS);

        //修改状态栏的颜色
        if (resIds[0] == 0) {
            int v7StatusBarColorID = resIds[2];
            if (v7StatusBarColorID != 0) {
                activity.getWindow().setStatusBarColor(SkinResources.getInstance().getColor(v7StatusBarColorID));
            }
        } else {
            activity.getWindow().setStatusBarColor(SkinResources.getInstance().getColor(resIds[0]));
        }
        //修改底部虚拟按键的颜色
        if (resIds[1] != 0) {
            activity.getWindow().setNavigationBarColor(SkinResources.getInstance().getColor(resIds[1]));
        }
    }

    /**
     * 获得字体
     *
     * @param activity
     */
    public static Typeface getSkinTypeface(Activity activity) {
        //获取app的字体ID
        int skinTypeceId = getResID(activity, TYPEFACE_ATTR)[0];
        //获取皮肤包的字体
        Typeface typeface = SkinResources.getInstance().getTypeface(skinTypeceId);
        return typeface;
    }
}
